package com.wangyadong.hobby.schedule.jobtask.javasync.semphore;

import java.util.concurrent.Semaphore;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/1/1019:31
 **/
public class SemaphoreExample1 {

    static class SemaphoreLock {
        final Semaphore semaphore = new Semaphore(1);

        //申请一个许可证
        public void lock() throws InterruptedException {
            semaphore.acquire();
        }

        public void release() {
            semaphore.release();
        }
    }

    public static void main(String[] args) {
        final SemaphoreLock semaphore = new SemaphoreLock();
        new Thread(() -> {
            try {
                try {
                    semaphore.lock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } finally {
                semaphore.release();
            }
        }).start();

    }
}
